# encoding: utf-8
'''
Created on 2018-6-13
desription:数据库连接助手类
@author: zhujianhua
'''

import cx_Oracle
import MySQLdb
import sys
import time

sys.path.append('./helper')
from helper import InfoMonConstants
#import InfoMonConstants
from threading import Thread, Lock, Condition
import threading

class ConnectionHelper:
    conn = None
    _l = None
    def __init__(self, dbName):
        self._l = threading.Lock()
        self.conn = MySQLdb.connect(host=InfoMonConstants.HOST, user=InfoMonConstants.USER, passwd=InfoMonConstants.PASSWORD, db=InfoMonConstants.SID)

    def getConnection(self):
        return self.conn
    
    def getCursor(self):
        return self.conn.cursor()

    def execute(self, sql):
        if self._l.acquire():
            cursor = self.conn.cursor()
            cursor.execute(sql)
        self._l.release()
        return cursor.fetchall()

    def getValue(self, query):
        self._l.acquire()
        """ 查询相关参数 返回一个值 """
        cursor = self.conn.cursor()
        getNum = cursor.execute(query)
        if getNum > 0:
            result = cursor.fetchone()
        else:
            result = ['0']
        self._l.release()
        return int(result[1])

    def getQuery(self, query):
        """ 查询相关参数 返回多个值 """
        self._l.acquire()
        cursor = self.conn.cursor()
        cursor.execute(query)
        result = cursor.fetchone()
        self._l.release()
        for row in result:
            return int(row)

    # def getDiffValue(self, type):
    #     old_value = getValue(self, type)
    #     time.sleep(1)
    #     new_value = getValue(conn, type)
    #     return new_value - old_value
    #
    # def getDiffQuery(self, type):
    #     old_value = getQuery(type)
    #     time.sleep(1)
    #     new_value = getQuery(type)
    #     return new_value - old_value
